<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Http\Requests\Mini\Meter\CollectorMeter;
use App\Http\Resources\Estate\CollectorMeterResource\CollectorMeterCollection;
use App\Models\ElectricAlarmPlan;
use App\Models\ElectricChargingScheme;
use App\Models\ElectricCollector;
use App\Models\ElectricCommunity;
use App\Models\ElectricCommunityArea;
use App\Models\ElectricCommunityTower;
use App\Models\ElectricElectricity;
use App\Models\ElectricPlatformAgent;
use App\Services\CollectorMeterService;
use App\Services\ElectricMeterService;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class CollectorMeterController extends Controller
{
    /**
     * 2021/3/15--14:49
     * @effect:采集器列表
     * @param Request $request
     * @param CollectorMeterService $collector_service
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:index
     */
    public function index(Request $request, CollectorMeterService $collector_service)
    {

        $rs = $collector_service->get_list();
        return $rs['status'] ? $this->success(new CollectorMeterCollection($rs['data'])) : $this->error($rs['msg']);
    }

    /**
     * 2021/3/15--14:50
     * @effect:添加采集器
     * @param Request $request
     * @param CollectorMeterService $collector_service
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:store
     */
    public function store(CollectorMeter $request, CollectorMeterService $collector_service): array
    {
        $data = $collector_service->add($request);
        return $data['status'] ? $this->success([], __('base.success')) : $this->error($data['msg']);
    }

    /**
     * 2021/3/15--14:51
     * @effect:展示采集器数据
     * @param ElectricCollector $meter_model
     * @param $id
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:show
     */
    public function show(ElectricCollector $meter_model, $id)
    {
        $info = $meter_model->find($id);
        return $this->success($info);
    }

    /**
     * 2021/3/15--14:52
     * @effect:更新采集器数据
     * @param Request $request
     * @param CollectorMeterService $collector_service
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:update
     */
    public function update(Request $request, CollectorMeterService $collector_service)
    {
        $data = $collector_service->edit($request);
        return $data['status'] ? $this->success([], __('base.success')) : $this->error($data['msg']);
    }

    /**
     * 2021/3/15--14:52
     * @effect:删除采集器
     * @param ElectricCollector $meter_model
     * @param $id
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:destroy
     */
    public function destroy(ElectricCollector $meter_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        $meter_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }

    /**
     * 2021/3/15--14:53
     * @effect:获取区域，采集器，报警方案，缴费方案数组
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:get_community
     */
    public function get_community()
    {
        $community_arr = [];
        $community_db = ElectricCommunity::selectRaw('id value,name label')->where(['status' => 1])->get();
        if ($community_db->count()) {
            $community_arr = $community_db;
        }
        $collector_arr = [];
        $collector_db = ElectricCollector::selectRaw('id value,name label')->where(['status' => 1])->get();
        if ($collector_db->count()) {
            $collector_arr = $collector_db;
        }
        $alarm_arr = [];
        $alarm_db = ElectricAlarmPlan::selectRaw('id value,plan_title label')->where(['status' => 1])->get();
        if ($alarm_db->count()) {
            $alarm_arr = $alarm_db;
        }
        $plan_arr = [];
        $plan_db = ElectricChargingScheme::selectRaw('id value,plan_title label')->where(['status' => 1])->get();
        if ($plan_db->count()) {
            $plan_arr = $plan_db;
        }
        return $this->success(compact("collector_arr", "community_arr", "alarm_arr", "plan_arr"));
    }

    /**
     * 2021/3/15--17:09
     * @effect:获取数区域，代理商数组
     * @return array
     * @author:苏冬冬
     * @QQ:1299273928
     * @method:get_agent
     */
    public function get_agent()
    {
        $community_arr = ElectricCommunity::selectRaw('id value,name label')->where(['status' => 1])->get();
        $agent_arr = ElectricPlatformAgent::selectRaw('id value,agent_name label')->where(['status' => 1])->get();
        $area_arr = ElectricCommunityArea::selectRaw('id value,area_name label,c_id')->where(['status' => 1])->get();
        $tower_arr = ElectricCommunityTower::selectRaw('id value,number label,area_id')->where(['status' => 1])->get();
        return $this->success(compact('community_arr', 'agent_arr', "area_arr", "tower_arr"));
    }
}
